<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNum">
        <el-input v-model="queryParams.orderNum" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="任务" prop="taskId">
        <el-select v-model="queryParams.taskId" placeholder="选择任务" @change="handleSelectChange2" style="width: 100%;">
          <el-option v-for="option in ServerTaskList" :key="option.taskId" :value="option.taskId"
            :label="option.taskName">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="选择订单状态" @change="handleSelectChange3"
          style="width: 100%;">
          <el-option v-for="option in orderStatuList" :key="option.code" :value="option.code" :label="option.value">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column label="订单编号" align="center" prop="orderNum" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="价格" align="center" prop="orderPrice" />
      <el-table-column label="下单时间" align="center" prop="orderTime" width="180">
      </el-table-column>
      <el-table-column label="用户QQ号" align="center" prop="qqCode" />
      <el-table-column label="订单备注" align="center" prop="remark" />
      <el-table-column label="付款状态" align="center" prop="orderStatusName" />
      <el-table-column label="任务状态" align="center" prop="stateName" />
      <el-table-column label="订单完成时间" align="center" prop="orderFinishTime" width="180">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            v-hasPermi="['order:info:query']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号">
          <el-input v-model="form.orderNum" disabled />
        </el-form-item>
        <el-form-item label="订单价格">
          <el-input v-model="form.orderPrice" disabled />
        </el-form-item>
        <el-form-item label="实付金额">
          <el-input v-model="form.payAmount" placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="账号">
          <el-input v-model="form.account" disabled />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" show-password />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="form.orderStatus == '1'" type="primary" @click="manualPay">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, getServerTaskList, manualPay } from "@/api/order/info";

export default {
  name: "Info",
  data() {
    return {
      // 遮罩层
      loading: true,
      ServerTaskList: [],
      orderStatuList: [
        {
          code: 1,
          value: '未付款'
        },
        {
          code: 2,
          value: '已付款'
        },
        {
          code: 3,
          value: '已完成'
        },
        {
          code: 4,
          value: '已取消'
        }
      ],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNum: null,
        orderPrice: null,
        orderTime: null,
        orderTimeOut: null,
        payAmount: null,
        payTime: null,
        orderStatus: null,
        orderFinishTime: null,
        taskId: null,
        orderSnapshot: null,
        orderUserId: null,
        orderUserName: null,
        accountId: null,
        account: null,
        userDelFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNum: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        orderPrice: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
        orderTime: [
          { required: true, message: "下单时间不能为空", trigger: "blur" }
        ],
        orderTimeOut: [
          { required: true, message: "订单超时时间不能为空", trigger: "blur" }
        ],
        orderStatus: [
          { required: true, message: "订单状态:1:未付款；2：已付款；3：已完工不能为空", trigger: "change" }
        ],
        taskId: [
          { required: true, message: "任务id不能为空", trigger: "blur" }
        ],
        orderSnapshot: [
          { required: true, message: "订单快照不能为空", trigger: "blur" }
        ],
        orderUserId: [
          { required: true, message: "下单用户不能为空", trigger: "blur" }
        ],
        orderUserName: [
          { required: true, message: "下单用户名称不能为空", trigger: "blur" }
        ],
        accountId: [
          { required: true, message: "账号id不能为空", trigger: "blur" }
        ],
        account: [
          { required: true, message: "账号不能为空", trigger: "blur" }
        ],
        userDelFlag: [
          { required: true, message: "用户删除标识不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "删除标识不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.AgetServerTaskList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取任务
    AgetServerTaskList() {
      getServerTaskList().then(res => {
        this.ServerTaskList = res.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        orderNum: null,
        orderPrice: null,
        orderTime: null,
        orderTimeOut: null,
        payAmount: null,
        payTime: null,
        orderStatus: null,
        orderFinishTime: null,
        taskId: null,
        orderSnapshot: null,
        orderUserId: null,
        orderUserName: null,
        accountId: null,
        account: null,
        createBy: null,
        createTime: null,
        updateTime: null,
        userDelFlag: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getInfo(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看订单详情";
      });
    },
    /** 提交按钮 */
    manualPay() {
      console.log(this.form)
      manualPay(this.form).then(response => {
        console.log(response)
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + orderIds + '"的数据项？').then(function () {
        return delInfo(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    // 选择状态
    handleSelectChange2(taskId) {
      this.queryParams.taskId = taskId;
    },
    handleSelectChange3(code) {
      this.queryParams.orderStatus = code;
    },
  }
};
</script>
